#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define int long long
vector<pair<int, int>> a;
signed main() {
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        a.push_back({x, y});
    }
    sort(a.begin(), a.end());
    int len = 0;
    int l = a[0].first;
    int r = a[0].second;
    if(n == 1) {
        cout << r - l << endl;
        return 0;
    }
    for(int i = 1; i < n; i++) {
        if(a[i].first <= r) {
            if(a[i].second > r)
                r = a[i].second;
        }else {
            len += r - l;
            l = a[i].first;
            r = a[i].second;
        }
    }
    len += r - l;
    cout << len << endl;
    return 0;
}